package controllers

import (
	"encoding/json"

	"gitee.com/239103/api4cmdb/models"

	"github.com/astaxie/beego"
)

// Operations about Users
// type UserController struct {
// 	beego.Controller
// }

// OSController - Operations about OS
type OSController struct {
	beego.Controller
}

// @Title CreateUser
// @Description create users
// @Param	body		body 	models.User	true		"body for user content"
// @Success 200 {int} models.User.Id
// @Failure 403 body is empty
// @router / [post]
// func (u *UserController) Post() {
// 	var user models.User
// 	json.Unmarshal(u.Ctx.Input.RequestBody, &user)
// 	uid := models.AddUser(user)
// 	u.Data["json"] = map[string]string{"uid": uid}
// 	u.ServeJSON()
// }

// Post - add new OS
func (o *OSController) Post() {
	var os models.OSinfo
	json.Unmarshal(o.Ctx.Input.RequestBody, &os)
	oid := models.AddOS(os)
	o.Data["json"] = map[string]string{"oid": oid}
	o.ServeJSON()
}

// @Title GetAll
// @Description get all Users
// @Success 200 {object} models.User
// @router / [get]
// func (u *UserController) GetAll() {
// 	users := models.GetAllUsers()
// 	u.Data["json"] = users
// 	u.ServeJSON()
// }

// GetAll - return all os objects
func (o *OSController) GetAll() {
	oslist := models.GetAllOS()
	o.Data["json"] = oslist
	o.ServeJSON()
}

// @Title Get
// @Description get user by uid
// @Param	uid		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.User
// @Failure 403 :uid is empty
// @router /:uid [get]
// func (u *UserController) Get() {
// 	uid := u.GetString(":uid")
// 	if uid != "" {
// 		user, err := models.GetUser(uid)
// 		if err != nil {
// 			u.Data["json"] = err.Error()
// 		} else {
// 			u.Data["json"] = user
// 		}
// 	}
// 	u.ServeJSON()
// }

// Get - query a os
func (o *OSController) Get() {
	oid := o.GetString("oid")
	if oid != "" {
		os, err := models.GetOS(oid)
		if err != nil {
			o.Data["json"] = err.Error()
		} else {
			o.Data["json"] = os
		}
	} else {
		o.GetAll()
	}
	o.ServeJSON()
}

// @Title Update
// @Description update the user
// @Param	uid		path 	string	true		"The uid you want to update"
// @Param	body		body 	models.User	true		"body for user content"
// @Success 200 {object} models.User
// @Failure 403 :uid is not int
// @router /:uid [put]
// func (u *UserController) Put() {
// 	uid := u.GetString(":uid")
// 	if uid != "" {
// 		var user models.User
// 		json.Unmarshal(u.Ctx.Input.RequestBody, &user)
// 		uu, err := models.UpdateUser(uid, &user)
// 		if err != nil {
// 			u.Data["json"] = err.Error()
// 		} else {
// 			u.Data["json"] = uu
// 		}
// 	}
// 	u.ServeJSON()
// }

// Put - update the os
func (o *OSController) Put() {
	oid := o.GetString(":oid")
	if oid != "" {
		var os models.OSinfo
		json.Unmarshal(o.Ctx.Input.RequestBody, &os)
		oo, err := models.UpdateOS(oid, &os)
		if err != nil {
			o.Data["json"] = err.Error()
		} else {
			o.Data["json"] = oo
		}
	}
	o.ServeJSON()
}

// @Title Delete
// @Description delete the user
// @Param	uid		path 	string	true		"The uid you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 uid is empty
// @router /:uid [delete]
// func (u *UserController) Delete() {
// 	uid := u.GetString(":uid")
// 	models.DeleteUser(uid)
// 	u.Data["json"] = "delete success!"
// 	u.ServeJSON()
// }

// Delete - remove a os
func (o *OSController) Delete() {
	oid := o.GetString(":oid")
	models.DeleteOS(oid)
	o.Data["json"] = "delete success!"
	o.ServeJSON()
}

// @Title Login
// @Description Logs user into the system
// @Param	username		query 	string	true		"The username for login"
// @Param	password		query 	string	true		"The password for login"
// @Success 200 {string} login success
// @Failure 403 user not exist
// @router /login [get]
// func (u *UserController) Login() {
// 	username := u.GetString("username")
// 	password := u.GetString("password")
// 	if models.Login(username, password) {
// 		u.Data["json"] = "login success"
// 	} else {
// 		u.Data["json"] = "user not exist"
// 	}
// 	u.ServeJSON()
// }

// @Title logout
// @Description Logs out current logged in user session
// @Success 200 {string} logout success
// @router /logout [get]
// func (u *UserController) Logout() {
// 	u.Data["json"] = "logout success"
// 	u.ServeJSON()
// }
